/***************************************************
 @Name :        小铭同学
 @Time :        2025-09-03
 @Email :       LiaoMingWJ233@163.com
 @Comment :     水表 电表
 @FileName :    widget.cpp
 @Version :     1.0
****************************************************/


#include "widget.h"
#include "ui_widget.h"

Widget::Widget(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::Widget)
{
    ui->setupUi(this);
    
    ui->label_bj->setPixmap(QPixmap(":/主题背景"));
    
    ui->exit->raise();
    ui->exit->setStyleSheet(R"(
        QPushButton {
            background-color: #71afa0;  /* 按钮背景颜色 - 青绿色 */
            color: white;               /* 文字颜色 - 白色 */
            border-radius: 15px;        /* 边框圆角半径 - 15像素 */
            border: none;               /* 边框样式 - 无边框 */
            padding: 4px 4px;           /* 内边距 - 上下4像素，左右4像素 */
            font-size: 18px;            /* 字体大小 - 20像素 */
            font-weight: bold;          /* 字体粗细 - 粗体 */
            font-family: "楷体";        /* 字体家族 - 楷体 */
        }
        QPushButton:hover {
            background-color: #5d9c8b;  /* 鼠标悬停时的背景颜色 - 深青绿色 */
        }
        QPushButton:pressed {
            background-color: #4a8978;  /* 鼠标按下时的背景颜色 - 更深青绿色 */
        }
    )");    
    
    // 创建主布局 - 左右布局
    QHBoxLayout *mainLayout = new QHBoxLayout(this);
    mainLayout->setSpacing(20);
    mainLayout->setContentsMargins(20, 20, 20, 20);
    
    // ==================== 左侧水表区域 ====================
    QWidget *leftWidget = new QWidget(this);
    QVBoxLayout *leftLayout = new QVBoxLayout(leftWidget);
    leftLayout->setSpacing(10);
    leftLayout->setContentsMargins(0, 0, 0, 0);
    
    // 水表标题
    QLabel *waterTitle = new QLabel("水表监控", this);
    waterTitle->setAlignment(Qt::AlignCenter);
    waterTitle->setStyleSheet("QLabel { "
                             "font-size: 30px; "
                             "font-weight: bold; "
                             "color: #0066CC; "
                             "padding: 3px; "
                             "}");
    waterTitle->setFixedHeight(50);
    leftLayout->addWidget(waterTitle);
    
    // 创建水表
    waterMeter = new WaterElectricMeter(this);
    waterMeter->setMeterType(WaterElectricMeter::WaterMeter);
    waterMeter->setValue(25.5);
    waterMeter->setMinValue(0);
    waterMeter->setMaxValue(100);
    waterMeter->setUnit("m³");
    waterMeter->setDialColor(QColor(200, 230, 255));
    waterMeter->setNeedleColor(QColor(60, 140, 255));
    waterMeter->setTextColor(QColor(0, 80, 160));
    
    // 设置水表大小
    waterMeter->setFixedSize(280, 280);
    leftLayout->addWidget(waterMeter, 0, Qt::AlignHCenter);
    
    // 水表控制面板 - 简化版
    QWidget *waterControlWidget = new QWidget(this);
    waterControlWidget->setFixedHeight(80); // 限制控制面板高度
    QVBoxLayout *waterControlLayout = new QVBoxLayout(waterControlWidget);
    waterControlLayout->setSpacing(5);
    waterControlLayout->setContentsMargins(10, 5, 10, 5);
    
    // 水量输入框
    QHBoxLayout *waterInputLayout = new QHBoxLayout();
    QLabel *waterLabel = new QLabel("水量:", this);
    waterLabel->setFixedWidth(40);
    waterLabel->setStyleSheet("font-weight: bold; color: #0066CC;");
    
    waterSpinBox = new QSpinBox(this);
    waterSpinBox->setRange(0, 100);
    waterSpinBox->setValue(25);
    waterSpinBox->setSuffix(" m³");
    waterSpinBox->setFixedWidth(80);
    waterSpinBox->setStyleSheet("QSpinBox { "
                              "padding: 2px; "
                              "border: 1px solid #99CCFF; "
                              "border-radius: 3px; "
                              "}");
    
    waterInputLayout->addWidget(waterLabel);
    waterInputLayout->addWidget(waterSpinBox);
    waterInputLayout->addStretch();
    
    // 水量滑动条
    waterSlider = new QSlider(Qt::Horizontal, this);
    waterSlider->setRange(0, 100);
    waterSlider->setValue(25);
    waterSlider->setFixedHeight(20);
    waterSlider->setStyleSheet("QSlider::groove:horizontal {"
                             "border: 1px solid #999999;"
                             "height: 6px;"
                             "background: #E6F3FF;"
                             "margin: 0px 0;"
                             "border-radius: 3px;"
                             "}"
                             "QSlider::handle:horizontal {"
                             "background: #0066CC;"
                             "border: 1px solid #5c5c5c;"
                             "width: 12px;"
                             "margin: -3px 0;"
                             "border-radius: 6px;"
                             "}");
    
    waterControlLayout->addLayout(waterInputLayout);
    waterControlLayout->addWidget(waterSlider);
    leftLayout->addWidget(waterControlWidget);
    leftLayout->addStretch(); // 添加弹性空间
    
    // ==================== 右侧电表区域 ====================
    QWidget *rightWidget = new QWidget(this);
    QVBoxLayout *rightLayout = new QVBoxLayout(rightWidget);
    rightLayout->setSpacing(10);
    rightLayout->setContentsMargins(0, 0, 0, 0);
    
    // 电表标题
    QLabel *electricTitle = new QLabel("电表监控", this);
    electricTitle->setAlignment(Qt::AlignCenter);
    electricTitle->setStyleSheet("QLabel { "
                                "font-size: 30px; "
                                "font-weight: bold; "
                                "color: #CC3300; "
                                "padding: 3px; "
                                "}");
    electricTitle->setFixedHeight(50);
    rightLayout->addWidget(electricTitle);
    
    // 创建电表
    electricMeter = new WaterElectricMeter(this);
    electricMeter->setMeterType(WaterElectricMeter::ElectricMeter);
    electricMeter->setValue(75.3);
    electricMeter->setMinValue(0);
    electricMeter->setMaxValue(200);
    electricMeter->setUnit("kWh");
    electricMeter->setDialColor(QColor(255, 230, 200));
    electricMeter->setNeedleColor(QColor(255, 100, 100));
    electricMeter->setTextColor(QColor(160, 40, 0));
    
    // 设置电表大小
    electricMeter->setFixedSize(280, 280);
    rightLayout->addWidget(electricMeter, 0, Qt::AlignHCenter);
    
    // 电表控制面板 - 简化版
    QWidget *electricControlWidget = new QWidget(this);
    electricControlWidget->setFixedHeight(80); // 限制控制面板高度
    QVBoxLayout *electricControlLayout = new QVBoxLayout(electricControlWidget);
    electricControlLayout->setSpacing(5);
    electricControlLayout->setContentsMargins(10, 5, 10, 5);
    
    // 电量输入框
    QHBoxLayout *electricInputLayout = new QHBoxLayout();
    QLabel *electricLabel = new QLabel("电量:", this);
    electricLabel->setFixedWidth(40);
    electricLabel->setStyleSheet("font-weight: bold; color: #CC3300;");
    
    electricSpinBox = new QSpinBox(this);
    electricSpinBox->setRange(0, 200);
    electricSpinBox->setValue(75);
    electricSpinBox->setSuffix(" kWh");
    electricSpinBox->setFixedWidth(80);
    electricSpinBox->setStyleSheet("QSpinBox { "
                                 "padding: 2px; "
                                 "border: 1px solid #FF9999; "
                                 "border-radius: 3px; "
                                 "}");
    
    electricInputLayout->addWidget(electricLabel);
    electricInputLayout->addWidget(electricSpinBox);
    electricInputLayout->addStretch();
    
    // 电量滑动条
    electricSlider = new QSlider(Qt::Horizontal, this);
    electricSlider->setRange(0, 200);
    electricSlider->setValue(75);
    electricSlider->setFixedHeight(20);
    electricSlider->setStyleSheet("QSlider::groove:horizontal {"
                                "border: 1px solid #999999;"
                                "height: 6px;"
                                "background: #FFE6E6;"
                                "margin: 0px 0;"
                                "border-radius: 3px;"
                                "}"
                                "QSlider::handle:horizontal {"
                                "background: #CC3300;"
                                "border: 1px solid #5c5c5c;"
                                "width: 12px;"
                                "margin: -3px 0;"
                                "border-radius: 6px;"
                                "}");
    
    electricControlLayout->addLayout(electricInputLayout);
    electricControlLayout->addWidget(electricSlider);
    rightLayout->addWidget(electricControlWidget);
    rightLayout->addStretch(); // 添加弹性空间
    
    // ==================== 添加到主布局 ====================
    mainLayout->addWidget(leftWidget);
    mainLayout->addWidget(rightWidget);
    
    // 设置布局比例，左右各占50%
    mainLayout->setStretchFactor(leftWidget, 1);
    mainLayout->setStretchFactor(rightWidget, 1);
    
    // 连接信号槽
    connect(waterSlider, &QSlider::valueChanged, waterSpinBox, &QSpinBox::setValue);
    connect(waterSpinBox, QOverload<int>::of(&QSpinBox::valueChanged), waterSlider, &QSlider::setValue);
    connect(waterSlider, &QSlider::valueChanged, this, [this](int value) {
        waterMeter->setValue(value);
    });
    
    connect(electricSlider, &QSlider::valueChanged, electricSpinBox, &QSpinBox::setValue);
    connect(electricSpinBox, QOverload<int>::of(&QSpinBox::valueChanged), electricSlider, &QSlider::setValue);
    connect(electricSlider, &QSlider::valueChanged, this, [this](int value) {
        electricMeter->setValue(value);
    });
    
    // 可选：创建定时器模拟实时数据变化
    QTimer *timer = new QTimer(this);
    connect(timer, &QTimer::timeout, this, [this]() {
        static int counter = 0;
        counter++;
        
        // 每5秒微调一下数值，模拟实时变化
        if (counter % 5 == 0) {
            waterMeter->setValue(waterMeter->value() + 0.1);
            electricMeter->setValue(electricMeter->value() + 0.2);
        }
    });
    timer->start(1000); // 每秒触发一次
}

Widget::~Widget()
{
    delete ui;
}

void Widget::on_exit_clicked()
{
    this->close();
    QApplication::quit();
}
